package com.reremouse.servlet;

import com.reremouse.rerebbs.web.mb.ApplicationBean;
import com.reremouse.util.SpringHelper;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class ShowImage1
        extends HttpServlet {
    public void destroy() {
        super.destroy();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        FileInputStream is = null;

        String id = request.getParameter("id");
        if (id != null) {
            String relativePath = SpringHelper.getSpringBean("relativeDir").toString() + id + ".jpg";

            String path = request.getServletContext().getRealPath(relativePath);
            File fff = new File(path);
            if (fff.exists()) {
                System.out.println(relativePath);

                RequestDispatcher rd = getServletContext().getRequestDispatcher("/" + relativePath);
                rd.forward(request, response);
            } else {
                StringBuffer dir = new StringBuffer();
                ApplicationBean ab = (ApplicationBean) request.getServletContext().getAttribute("applicationBean");
                dir.append(ab.getFilePath());


                dir.append("\\");
                dir.append(id);
                String nfn = dir.toString();
                try {
                    is = new FileInputStream(nfn);
                } catch (Exception e) {
                    String fp = request.getRealPath("resources/sys/default.jpg");

                    is = new FileInputStream(fp);
                }
                OutputStream out = response.getOutputStream();
                response.setContentType("image/JPEG");
                try {
                    int bufLen = 1024;
                    byte[] buf = new byte[bufLen];
                    FileChannel channel = is.getChannel();
                    ByteBuffer byteBuffer = ByteBuffer.wrap(buf);
                    int n = 0;
                    int total = 0;
                    while ((n = channel.read(byteBuffer)) != -1) {
                        out.write(buf, 0, n);

                        byteBuffer.clear();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    is.close();
                    out.flush();
                    out.close();
                    response.setStatus(200);
                    response.flushBuffer();
                }
            }
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(getClass());
        out.println(", using the POST method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    public void init()
            throws ServletException {
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.servlet.ShowImage1

 * JD-Core Version:    0.7.0.1

 */